


public class Test {


    // 求斐波那契数列的第 N 项
    // 在数学上，斐波那契数列以如下被以递推的方法定义：
    // F(0)=0，F(1)=1, F(n)=F(n - 1)+F(n - 2)（n ≥ 2，n ∈ N*）

    public static int fib2(int n) {
        if(n == 0) {
            return 0;
        }
        if (n == 1 || n == 2) {
            return 1;
        }

        int last1 = 1;
        int last2 = 1;
        int cur = 0;
        for (int i = 3; i <= n; i++) {
            cur = last1 + last2;
            last1 = last2;
            last2 = cur;
        }
        return cur;
    }

    public static void main(String[] args) {
        System.out.println(fib2(40));
    }


    public static int count = 0; // 这个是类的成员变量. 后面会详细介绍到
    public static void main10(String[] args) {
        System.out.println(fib(40));
        System.out.println(count);
    }
    public static int fib(int n) {
        if (n == 0) {
            return 0;
        }
        if (n == 1 || n == 2) {
            return 1;
        } if
        (n == 3) {
            count++;
        }
        return fib(n - 1) + fib(n - 2);
    }



    /*public static int fib(int n) {
        if (n == 0) {
            return 0;
        }
        if (n == 1 || n == 2) {
            return 1;
        }
        return fib(n - 1) + fib(n - 2);
    }

    public static void main(String[] args) {
        System.out.println(fib(3));
        System.out.println(fib(6));
        System.out.println(fib(40));
    }*/


    // 写一个递归方法，输入一个非负整数，返回组成它的数字之和.
    // 例如，输入 1729, 则应该返回1+7+2+9，它的和是19
    public static int addSum(int num) {
        if (num < 10) {
            return num;
        }
        return num % 10 + addSum(num / 10);
    }


    public static void main9(String[] args) {
        System.out.println(addSum(1729));
    }


    // 递归求 1 + 2 + 3 + ... + 10
    public static int sum(int num) {
        if (num == 1) {
            return 1;
        }
        return num + sum(num - 1);
    }

    public static void main8(String[] args) {
        System.out.println(sum(10));
    }


    //按顺序打印一个数字的每一位(例如 1234 打印出 1 2 3 4)
    public static void print(int num) {
        if (num < 10) {
            System.out.println(num % 10);
            return;
        }
        //123
        print(num / 10);
        System.out.println(num % 10);
    }

    public static void main7(String[] args) {
        print(1234);
    }


    //递归求N的阶乘
    public static int factor(int n) {
        System.out.println("函数开始, n = " + n);
        if (n == 1) {                // 结束递归的条件
            System.out.println("函数结束, n = 1 ret = 1");
            return 1;
        }
        int ret = n * factor(n - 1); // 递推公式 —— 趋近于终止条件
        System.out.println("函数结束, n = " + n + " ret = " + ret);
        return ret;
    }


    public static void main6(String[] args) {
        System.out.println(factor(5));

    }


    public static void func(int a) {
        if (a == 1) {
            return;
        }
        System.out.println(a);
        func(a - 1);
    }


    // 递归
    public static void main5(String[] args) {
        func(10);

    }


    // 方法的重载
    public static int add(int a, int b) {
        return a + b;
    }

    public static int add(int a, int b, int c) {
        return a + b + c;
    }

    public static double add(double a, double b) {
        return a + b;
    }

    public static void main4(String[] args) {
        System.out.println(add(1, 2));
        System.out.println(add(1.2, 2.3));
        System.out.println(add(1, 2, 3));


//        int x = 10;
//        int y = 20;
//        System.out.println(add(x, y));
//
//        double d1 = 12.3;
//        double d2 = 45.6;
//
//        System.out.println(add(d1, d2));


    }


    // 交换
    public static void swap(int x, int y) {

        int tmp = x;
        x = y;
        y = tmp;
        System.out.println("swap: x = " + x + " y = " + y);
    }

    public static void main3(String[] args) {
        int a = 10;
        int b = 20;
        System.out.println("交换前：" + "a = " + a + " b = " + b);
        swap(a, b);
        System.out.println("交换后：" + "a = " + a + " y = " + b);
    }


    /**
     * 求 n 的阶乘
     *
     * @param n
     * @return
     */
    public static int fac(int n) {
        int ret = 1;
        for (int i = 1; i <= n; i++) {
            ret *= i;
        }
        return ret;
    }


    /**
     * 求 k 的阶乘和
     *
     * @param k
     * @return
     */
    public static int facSum(int k) {
        int sum = 0;
        for (int i = 1; i <= k; i++) {
            sum += fac(i);
        }
        return sum;
    }

    public static void main2(String[] args) {
        System.out.println(facSum(3));
        System.out.println(facSum(5));
    }







    /*public static 返回值 方法名 (形式参数列表) {
        方法体;
    }*/

    public static int sum(int a, int b) {
        return a + b;
    }

    public static void main1(String[] args) {
        int x = 10;
        int y = 20;
        int ret = sum(x, y);
        System.out.println(ret);

    }


}
